package org.goatsoft.catchpeaks;

public class Util {

	public static boolean isValidMove(Card[] state, int move) {
		boolean result = true;
		if (move < 0 || move >= state.length) {
			result = false;
		} else if (state[move] == null) {
			result = false;
		} else if (isFaceDown(state, move)) {
			result = false;
		} else if (move < state.length - 1 && !isAdjacentCard(state[move], state[state.length - 1])) {
			result = false;
		}
		return result;
	}

	public static boolean isAdjacentCard(Card a, Card b) {
		int diff = Math.abs(a.getValue().ordinal() - b.getValue().ordinal());
		return diff == 1 || diff == 12;
	}

	public static boolean isFaceDown(Card[] state, int i) {
		boolean result = state[i] != null && ((LEFT_COVER[i] != -1 && state[LEFT_COVER[i]] != null) || (RIGHT_COVER[i] != -1 && state[RIGHT_COVER[i]] != null));
		return result;
	}

	private static final byte[] LEFT_COVER = { 3, 5, 7, 9, 10, 12, 13, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };

	private static final byte[] RIGHT_COVER = { 4, 6, 8, 10, 11, 13, 14, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
}
